package com.java.basic;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest {

    public  static  void main(String[] args){
        Semaphore semaphore = new Semaphore(10);
        for(int i=0;i<15;i++){
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+" 获取许可++++");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                semaphore.release();
                System.out.println(Thread.currentThread().getName()+" 释放许可----");
            },"线程"+i).start();
        }
    }
}
